import { ArgumentsHost, Catch, ExceptionFilter, HttpException } from '@nestjs/common';
import { Request, Response } from 'express';
import { logger } from 'src/middleware/logger.middleware';
import { Logger } from '../utils/log4js';

@Catch()
export class HttpExceptionFilter implements ExceptionFilter {
	catch(exception: HttpException, host: ArgumentsHost) {
		const ctx = host.switchToHttp()
		const response = ctx.getResponse<Response>()
		const request = ctx.getRequest<Request>()
		const status = exception.getStatus()

		const logFormat = ` <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
		Request original url: ${request.originalUrl}
		Method: ${request.method}
		IP: ${request.ip}
		Status code: ${status}
		Response: ${exception.toString()} \n  <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
		`

		Logger.info(logFormat)
		response.status(status).json({
			statusCode: status,
			error: exception.message,
			msg: `${status >= 500 ? 'Service Error' : 'Client Error'}`
		})
	}
}
